
/*
 * 06.05.2007 niXman, i dot nixman at gmail dot com
 * UP 10.02.2009
 * UP 24.07.2009
 * UP 25.07.2009
 * UP 06.11.2009
 * UP 01.05.2011
 * UP 22.12.2011
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA.
 *
 */

#define SILENT (1)

#include "mfio.h"
#include <assert.h>

#define N (1024*32)

//---------------------------------------------------------------------------

int main(int argc, char* argv[]) {
   (void)argc; (void)argv;

   #ifdef __cplusplus
   using namespace mfio;
   #endif

   FILE* mfio_file = NULL;
   FILE* mfio_file1 = NULL;
   char str[256] = "\0";
   int i = 0, num = 0, size = 0;

   mfio_file = mfopen("1234.file", "wb");
   assert(mfio_file);
   printf("\nAfter: mfopen( \"1234.file\", \"wb\" );\n");
   report(mfio_file);

   for ( i = 0; i < N; i++ ) {
      size += mfwrite(&num, 1, 4, mfio_file);
      num++;
   }
   printf("\nAfter: mfwrite(&num, 1, 4, mfio_file); 32768 calls.\n");
   report(mfio_file);

   set_mode("rb", mfio_file);
   printf("\nAfter: set_open_mode(\"rb\", mfio_file);\n");
   report(mfio_file);

   mfseek(mfio_file, 0, SEEK_SET);
   printf("\nAfter: mfseek(mfio_file, 0, SEEK_SET);\n");
   report(mfio_file);

   for ( i = 0; i < N; i++ ) {
      mfread(&num, 1, 4, mfio_file);
      assert(num == i);
   }

   printf("\nAfter: mfread(&num, 1, 4, mfio_file); 32768 calls.\n");
   report(mfio_file);

   mfio_file1 = load_from_file("mfio.h");
   printf("\nAfter: load_from_file( \"mfio.h\" );\n");
   report(mfio_file1);

   do {
      mfgets(str, sizeof(str), mfio_file1);
   } while ( mfeof(mfio_file1) == 0 );
   printf("\nAfter calls: mfgets( str, sizeof( str ), mfio_file1 ); while not end of file.\n");
   report(mfio_file1);

   resize(mfio_file1, 1024);
   printf("\nAfter: resize( mfio_file1, 1024 );\n");
   report(mfio_file1);

   mfseek(mfio_file1, 0, SEEK_SET);
   printf("\nAfter: mfseek( mfio_file1, 0, SEEK_SET );\n");
   report(mfio_file1);

   do {
      mfgets(str, sizeof(str), mfio_file1);
   } while ( mfeof(mfio_file1) == 0 );
   printf("\nAfter: mfgets( str, sizeof(str), mfio_file1 ); while not end of file.\n");
   report(mfio_file1);

   truncate(mfio_file1);
   printf("\nAfter: truncate( mfio_file1 );\n");
   report(mfio_file1);

   mfclose(mfio_file1);

   mfio_file1 = load_from_file("mfio.h");
   printf("\nAfter: load_from_file( \"mfio.h\" );\n");
   report(mfio_file1);

   cat_file(mfio_file, mfio_file1);
   printf("\nAfter: cat_file( mfio_file, mfio_file1 );\n");
   report(mfio_file);

   save_to_file("output.bin", mfio_file);
   printf("\nAfter: save_to_file( \"output.bin\", mfio_file );\n");
   report(mfio_file);

   mfclose(mfio_file);
   mfclose(mfio_file1);

   return 0;
}

//---------------------------------------------------------------------------
